//
//  ZFK_postView.m
//  Drops
//
//  Created by SHG on 16/5/25.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "ZFK_postView.h"
#import "ZFK_postCollectionViewCell.h"
#import "UIKit+AFNetworking.h"
//#import "ZFKFlowLayout.h"

@implementation ZFK_postView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
        [self.mainCollectionView reloadData];
    }
    return self;
}


- (void)createView {
    ZFKFlowLayout *layout = [[ZFKFlowLayout alloc] init];
    layout.columnCounts = 2;
    layout.colunmSpace = 10;
    layout.rowSpace = 10;
    layout.edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.delegate = self;
    
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:layout];
    NSLog(@"+++%f", self.frame.size.height);
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.mainCollectionView];
    
    [self.mainCollectionView registerClass:[ZFK_postCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ZFK_postCollectionViewCell class])];
    
    self.mainCollectionView.scrollEnabled = NO;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    NSLog(@"count:-----%ld", self.postArray.count);
    NSLog(@"*&&&&%@", self.postArray);
    return self.postArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZFK_postCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ZFK_postCollectionViewCell class]) forIndexPath:indexPath];
    ZFK_post *post = self.postArray[indexPath.row];
    cell.post = post;
    [cell.imageView setImageWithURL:[NSURL URLWithString:post.postImg]];
    NSLog(@"[%@]", post.postImg);
    return cell;
}

- (CGFloat)layout:(ZFKFlowLayout *)layout heightForItemAtIndexPath:(NSIndexPath *)indexPath width:(CGFloat)width {
    ZFK_post *post = self.postArray[indexPath.row];
    CGFloat height = width * [post.postImgHeight floatValue] / [post.postImgWidth floatValue];
    return height;
}

- (void)setPostArray:(NSMutableArray *)postArray {
    _postArray = postArray;
}

- (void)setPost:(ZFK_post *)post {
    _post = post;
}

@end
